package com.ctn.bootstarp.tags.ui;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspTag;

import tldgen.BodyContentType;
import tldgen.Tag;
import tldgen.TagAttribute;

import com.ctn.bootstarp.tags.AppTag;
import com.ctn.bootstarp.tags.util.TagUtil;


/**
 * @author weiqi
 * @date 2013年8月7日
 */
@Tag(bodyContentType=BodyContentType.JSP)
public class TabTag extends AppTag {
	
	private static final long serialVersionUID = -4898118756098512742L;

	private TabPanelTag parentTag;
	
	private String title;
	
	@TagAttribute(required=true,description="this tab's title")
	public void setTitle(String title) {
		this.title = title;
	}
	
	private String code;
	
	@TagAttribute(required=true,description="this tab indentity code(also html element id)")
	public void setCode(String code) {
		this.code = code;
	}

	private Boolean active;
	
	@TagAttribute(description="the actived tab,only one can set true,default:false!")
	public void setActive(Boolean active) {
		this.active = active;
	}

	@Override
	public int doStartTag() throws JspException {
		JspTag parentTag = this.getParent();
		if(null==parentTag || !(parentTag instanceof TabPanelTag)){
			try {
				pageContext.getOut().write(TagUtil.printError("Tag标签需要放入TabPanel标签中"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		this.parentTag = (TabPanelTag) parentTag;
		return EVAL_BODY_BUFFERED;
	}
	

	@Override
	public int doAfterBody() throws JspException {
		String rawBody = this.getBodyContent().getString();
		TabItem item = new TabItem(active, title, code, rawBody);
		parentTag.addItem(item);
		return super.doAfterBody();
	}
	
	@Override
	public void release() {
		active = null;
		code = null;
		parentTag = null;
		title = null;
		super.release();
	}
	
}
